﻿namespace KeRui.Upload
{
    using System;
    using System.Text;
    using System.Web;

    internal class HTTPUtils
    {
        public static string Decode(string inValue)
        {
            string str = HttpUtility.UrlDecode(inValue, Encoding.ASCII);
            if (inValue != str)
            {
                return str;
            }
            str = HttpUtility.HtmlDecode(inValue);
            if (inValue != str)
            {
                return str;
            }
            return inValue;
        }

        public static string Decode(byte[] inValue, int inCodePage)
        {
            if (inValue == null)
            {
                return "";
            }
            return Decode(Encoding.GetEncoding(inCodePage).GetString(inValue));
        }

        public static string GetRawPath(byte[] inPath, int inCodePage)
        {
            return Decode(inPath, inCodePage);
        }

        public static string GetURLSafeFileName(byte[] inPath, int inCodePage)
        {
            string str = Decode(inPath, inCodePage);
            if (IsMacPath(str))
            {
                return MakeURLSafeFileName(str);
            }
            return MakeURLSafeFileName(Utilities.FindFileName(str));
        }

        public static string GetWinSafeFileName(byte[] inPath, int inCodePage)
        {
            string str = Decode(inPath, inCodePage);
            if (IsMacPath(str))
            {
                return MakeMacFileName(str);
            }
            return MakeSafeFileName(Utilities.FindFileName(str));
        }

        private static bool IsAlphaNum(char inChar)
        {
            return (((inChar >= 'a') && (inChar <= 'z')) || (((inChar >= 'A') && (inChar <= 'Z')) || ((inChar >= '0') && (inChar <= '9'))));
        }

        private static bool IsMacPath(string inPath)
        {
            return ((((inPath != null) && (inPath.Length > 0)) && (inPath[0] != '\\')) && (inPath.IndexOf(':') < 0));
        }

        private static string MakeMacFileName(string inName)
        {
            int index = 0;
            char[] chArray = inName.ToCharArray();
            index = 0;
            while (index < chArray.Length)
            {
                switch (chArray[index])
                {
                    case '"':
                        chArray[index] = (char)0xf020;
                        break;

                    case '*':
                        chArray[index] = (char)0xf021;
                        break;

                    case '/':
                        chArray[index] = (char)0xf022;
                        break;

                    case '|':
                        chArray[index] = (char)0xf027;
                        break;

                    case '\x00f0':
                        chArray[index] = (char)0xf02a;
                        break;

                    case ':':
                        chArray[index] = (char)0xf022;
                        break;

                    case '<':
                        chArray[index] = (char)0xf023;
                        break;

                    case '>':
                        chArray[index] = (char)0xf024;
                        break;

                    case '?':
                        chArray[index] = (char)0xf025;
                        break;

                    case '\\':
                        chArray[index] = (char)0xf026;
                        break;

                    default:
                        if (chArray[index] <= '\x001f')
                        {
                            chArray[index] = (char) (chArray[index] + 0xf000);
                        }
                        break;
                }
                index++;
            }
            if ((index > 0) && (chArray[index - 1] == ' '))
            {
                chArray[index - 1] = (char)0xf028;
            }
            else if ((index > 0) && (chArray[index - 1] == '.'))
            {
                chArray[index - 1] = (char)0xf029;
            }
            return new string(chArray);
        }

        private static string MakeSafeFileName(string inName)
        {
            int index = 0;
            char[] chArray = inName.ToCharArray();
            for (index = 0; index < chArray.Length; index++)
            {
                char ch = chArray[index];
                if (ch <= '/')
                {
                    switch (ch)
                    {
                        case '"':
                            goto Label_0087;
                           
                        case '*':
                            goto Label_0087;
                        
                            case '/':
                            goto Label_0087;
                           
                    }
                }
                else
                {
                    switch (ch)
                    {
                        case ':':
                        case '<':
                        case '>':
                        case '?':
                            goto Label_0080;

                        case ';':
                        case '=':
                            goto Label_0087;
                    }
                    if ((ch != '\\') && (ch != '|'))
                    {
                        goto Label_0087;
                    }
                }
            Label_0080:
                chArray[index] = '_';
                continue;
            Label_0087:
                if (chArray[index] < ' ')
                {
                    chArray[index] = '_';
                }
            }
            index = chArray.Length - 1;
            while (index >= 0)
            {
                if ((chArray[index] != ' ') && (chArray[index] != '.'))
                {
                    break;
                }
                index--;
            }
            return new string(chArray, 0, index + 1);
        }

        private static string MakeURLSafeFileName(string inName)
        {
            int index = 0;
            char[] chArray = inName.ToCharArray();
            for (index = 0; index < chArray.Length; index++)
            {
                if (IsAlphaNum(chArray[index]))
                {
                    chArray[index] = char.ToLower(chArray[index]);
                }
                else if ((chArray[index] != '-') && (chArray[index] != '.'))
                {
                    chArray[index] = '_';
                }
            }
            return new string(chArray);
        }
    }
}

